﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Frm_Paiement_Fournisseurs
    Public num = -1
    Public numf = -1
    Public modif = 0
    Public Sub reste()
        txt_rest.Clear()

        If txt_total_paye.Text <> "" And txt_prix.Text <> "" Then
            txt_rest.Text = CDbl(txt_prix.Text) - CDbl(txt_total_paye.Text)
        End If

    End Sub
    Public Sub total_paye()
        If Con.State = ConnectionState.Open Then
            cnClose()
        End If

        Try
            Cmd = New SqlCommand("Select * From Fournisseus", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()

                If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    num = Dr("Id_Fournisseur")
                ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    num = Dr("Id_Fournisseur")
                End If
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try

        Try
            txt_total_paye.Clear()
            Cmd = New SqlCommand("select Montant  from Fournisseurs_Paiement where Id_Fournisseur=" & numf & " and id_demande_client =" & id_event, Con)
            open()
            Dr = Cmd.ExecuteReader()
            If Dr.HasRows = True Then
                Dr.Close()
                Cmd = New SqlCommand("select sum(Montant) as sume from Fournisseurs_Paiement where Id_Fournisseur=" & numf & " and id_demande_client =" & id_event, Con)
                Dr = Cmd.ExecuteReader
                Dr.Read()
                txt_total_paye.Text = Dr("sume")
            End If
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Public Sub vider()

        txt_prix.Clear()
        If Comb_Nom_Fournisseurs.Items.Count > 0 Then
            Comb_Nom_Fournisseurs.SelectedIndex = 0
        End If

        If Comb_catégor.Items.Count > 0 Then
            Comb_catégor.SelectedIndex = 0
        End If

    End Sub
    Public Sub affi()
        Try
            Comb_Nom_Fournisseurs.Items.Clear()
            '  Cmd = New SqlCommand("select distinct Nom,Prenom,Nom_entreprise,Type_Fournisseur from Fournisseus, Fournisseurs_Demande where Fournisseus.Id_Fournisseur=Fournisseurs_Demande.Id_Fournisseur and Fournisseurs_Demande.Date_Reservation ='" & Comb_date_res.SelectedItem & "' and Fournisseurs_Demande.Salle ='" & Comb_salle.SelectedItem & "'", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                If Dr("Type_Fournisseur") = "Entreprise" Then
                    Comb_Nom_Fournisseurs.Items.Add(Dr("Nom_entreprise"))

                Else
                    Comb_Nom_Fournisseurs.Items.Add(Dr("Nom") & "  " & Dr("Prenom"))

                End If
            Loop
            Dr.Close()
            cnClose()
            If Comb_Nom_Fournisseurs.Items.Count > 0 Then
                Comb_Nom_Fournisseurs.SelectedIndex = 0
            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Public Sub affiche_paye()
        cnClose()
        Try
            Cmd = New SqlCommand("Select * From Fournisseus", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                End If
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        Try
            List_tous.Items.Clear()
            Cmd = New SqlCommand("Select * From Fournisseurs_Paiement Where Id_Fournisseur =" & numf & " and id_demande_client =" & id_event, Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Montant"), Dr("Date_m"), Dr("Type_Paiement"), Dr("id_piem"), Dr.GetInt32(7)})
                List_tous.Items.Add(Line)
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Private Sub Frm_Paiement_Fournisseurs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Pour l'archivage du team
        cmbTypePay.SelectedIndex = 0
    End Sub

    Private Sub Comb_Nom_Fournisseurs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_Nom_Fournisseurs.SelectedIndexChanged
        If Con.State = ConnectionState.Open Then
            cnClose()
        End If

        Try
            Cmd = New SqlCommand("Select * From Fournisseus", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()

                If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                End If
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try

        Try
            Comb_catégor.Items.Clear()
            Cmd = New SqlCommand("Select Categorie From Fournisseus Where Id_Fournisseur =" & numf, Con)
            open()
            Dr = Cmd.ExecuteReader()
            If Dr.HasRows Then
                Do While Dr.Read
                    Comb_catégor.Items.Add(Dr("Categorie"))
                Loop
            End If

            Dr.Close()
            cnClose()

            If Comb_catégor.Items.Count > 0 Then
                Comb_catégor.SelectedIndex = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        '------------------------------------------------------------------------------------------------------------------------------------
        Try
            txt_prix.Clear()
            Cmd = New SqlCommand("select sum(Prix * Quantite) as prix from fourniture, Fournisseurs_Demande where fourniture.id_demande=Fournisseurs_Demande.id_demande and Fournisseurs_Demande.Id_Fournisseur=" & numf & " and Fournisseurs_Demande.id_demande_client =" & id_event, Con)
            open()
            If Cmd.ExecuteScalar() IsNot DBNull.Value Then
                txt_prix.Text = CDbl(Cmd.ExecuteScalar())
            Else
                txt_prix.Text = 0
            End If
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try

        '_____________________________________________________________________'

        affiche_paye()
        total_paye()
        reste()
    End Sub

    Private Sub btn_supp_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp_paye.Click
        Try
            Dim n As Integer
            With List_tous
                If .SelectedIndices.Count > 0 Then
                    For i As Integer = 0 To .SelectedIndices.Count - 1
                        n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                    Next
                    If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Suppression") = MsgBoxResult.Yes Then
                        Try
                            Cmd = New SqlCommand("Delete From Fournisseurs_Paiement Where id_piem =" & n, Con)
                            open()
                            Cmd.ExecuteNonQuery()
                            'Tarik
                            'Supprimer le paiments d'héritage
                            Cmd = New SqlCommand("delete from paiements where idPai = " & idPai, Con)
                            Cmd.ExecuteNonQuery()
                            cnClose()
                            modif = 0
                            MsgBox("Montant Supprimer ", MsgBoxStyle.Information, "é modif Réussi !")
                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            cmbTypePay.SelectedIndex = 0
                            For Each it As ListViewItem In List_tous.Items
                                it.Selected = False
                            Next

                            affiche_paye()
                            total_paye()
                            reste()

                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                            cnClose()
                        End Try
                    End If
                Else
                    MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "Réssayer !")
                End If
            End With
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub

    Private Sub btn_ajouter_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter_paye.Click
        If txt_mont_paye.Text <> "" And Comb_Nom_Fournisseurs.Text <> "" Then

            Try
                Cmd = New SqlCommand("Select * From Fournisseus", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()

                    If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                        num = Dr("Id_Fournisseur")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                        num = Dr("Id_Fournisseur")
                    End If
                Loop
                Dr.Close()
                cnClose()


                If txt_mont_paye.Text <> "" Then
                    If cmbTypePay.SelectedIndex = 1 Then
                        If IsNothing(cheq.numChe) Then
                            MsgBox("Vous n'avez pas rempli le Chéque!!!", MsgBoxStyle.Critical, "Erreur")
                            Exit Sub
                        End If
                    End If
                    If cmbTypePay.SelectedIndex = 2 Then
                        If IsNothing(effetObj.numEffet) Then
                            MsgBox("Vous n'avez pas rempli l'Effet!!!", MsgBoxStyle.Critical, "Erreur")
                            Exit Sub
                        End If
                    End If

                    If cmbTypePay.SelectedIndex = 3 Then
                        If IsNothing(objVirement.numCompte) Then
                            MsgBox("Vous n'avez pas rempli le virement!!!", MsgBoxStyle.Critical, "Erreur")
                            Exit Sub
                        End If
                    End If
                    If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then
                        Try
                            'Tarik
                            'Ajouter le paiement dans la table paiement qui a une relation avec le Chéque et l'effet
                            Cmd = New SqlCommand("INSERT INTO [paiements]([montant],[type])VALUES(@montant,@type)", Con)
                            Cmd.Parameters.Add("@montant", SqlDbType.Float)
                            Cmd.Parameters("@montant").Value = Math.Round(CDbl(txt_mont_paye.Text), 2)
                            Cmd.Parameters.Add("@type", SqlDbType.VarChar)
                            Cmd.Parameters("@type").Value = cmbTypePay.Text
                            open()
                            Cmd.ExecuteNonQuery()

                            Cmd.CommandText = "select @@IDENTITY"
                            idPai = Cmd.ExecuteScalar
                            'End 

                            Cmd.CommandText = "insert into Fournisseurs_Paiement (Id_Fournisseur,id_demande_client,Montant,Date_m,Remarque,Type_Paiement,idPai)" &
                            " values (@Id_Fournisseur,@id_demande_client,@Montant,@Date_m,@Remarque,@Type_Paiement," & idPai & ")"
                            Cmd.Parameters.Clear()
                            Cmd.Parameters.Add("@Id_Fournisseur", SqlDbType.Int).Value = num
                            Cmd.Parameters.Add("@id_demande_client", SqlDbType.Int).Value = id_event
                            Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = txt_mont_paye.Text
                            Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_paye.Value.Date
                            Cmd.Parameters.Add("@Type_Paiement", SqlDbType.VarChar).Value = cmbTypePay.Text
                            Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_paye.Text
                            Cmd.ExecuteNonQuery()

                            'Tarik
                            Dim idNomin As Integer 'pour stocker la valeur de l'idNominatif ajouté
                            If Not IsNothing(nomin.nom) Then
                                Cmd.Parameters.Clear()
                                'cette partie ajoute le nominatif et retourne l'id de ce dernier
                                Cmd.CommandText = "ajouterNominatif"
                                Cmd.CommandType = CommandType.StoredProcedure
                                Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                                Cmd.Parameters("@nom").Value = nomin.nom
                                Cmd.Parameters.Add("@Prenom", SqlDbType.VarChar)
                                Cmd.Parameters("@Prenom").Value = nomin.prenom
                                Cmd.Parameters.Add("@idNomin", SqlDbType.Int)
                                Cmd.Parameters("@idNomin").Direction = ParameterDirection.Output
                                Cmd.ExecuteNonQuery()
                                idNomin = Cmd.Parameters("@idNomin").Value
                            End If

                            'Ajouter le Chéque ou l'effet
                            If cmbTypePay.SelectedIndex = 2 Then
                                If Not IsNothing(effetObj.numEffet) Then
                                    Cmd = New SqlCommand("INSERT INTO [Effet] " & _
                                                    "VALUES(" & idPai & ",@numChe,'" & Format(effetObj.dteEffet, "MM-dd-yyyy") & "'," & nullPourDate(effetObj.dteRetrait) & ",'" & Format(effetObj.dateEcheance, "MM-dd-yyyy") & "'," & convertToBit(cheq.nominatif) & ",@banqueDestinataire,@banqueDestinateur,'F')", Con)
                                    'MsgBox(cheq.dteRetra)
                                    Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                                    Cmd.Parameters("@numChe").Value = effetObj.numEffet

                                    Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinataire").Value = effetObj.banqueDestinataire

                                    Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinateur").Value = effetObj.banqueDestinateur

                                    Cmd.ExecuteNonQuery()
                                    If Not IsNothing(nomin.nom) Then
                                        Cmd.CommandText = "insert into nominEffet values(" & idNomin & "," & idPai & ")"
                                        Cmd.ExecuteNonQuery()
                                    End If


                                    effetObj = Nothing
                                    nomin = Nothing
                                    idNomin = 0
                                    idPai = 0
                                End If
                            ElseIf cmbTypePay.SelectedIndex = 1 Then
                                If Not IsNothing(cheq.numChe) Then
                                    Cmd = New SqlCommand("INSERT INTO [cheques] " & _
                                                     "VALUES(" & idPai & ",@numChe," & convertToBit(cheq.nominatif) & "," & nullPourDate(cheq.dteRetra) & ",'" & Format(cheq.dateChequ, "MM-dd-yyyy") & "',@banqueDestinataire,@banqueDestinateur,'F')", Con)
                                    'MsgBox(cheq.dteRetra)
                                    Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                                    Cmd.Parameters("@numChe").Value = cheq.numChe

                                    Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinateur").Value = cheq.banqueDestinateur

                                    Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinataire").Value = cheq.banqueDestinataire
                                    Cmd.ExecuteNonQuery()

                                    If Not IsNothing(nomin.nom) Then
                                        Cmd.CommandText = "insert into nominCheq values(" & idPai & "," & idNomin & ")"
                                        Cmd.ExecuteNonQuery()
                                    End If
                                    cheq = Nothing
                                    nomin = Nothing
                                    idNomin = 0
                                    idPai = 0
                                End If
                            ElseIf cmbTypePay.SelectedIndex = 3 Then
                                If Not IsNothing(objVirement.numCompte) Then
                                    Cmd = New SqlCommand("INSERT INTO [virement] " &
                                                        "VALUES (" & idPai & ",@numCompte,@banqueDestinataire, " &
                                                        "@banqueDestinateur,'" & Format(objVirement.dateVirement, "MM-dd-yyyy") & "',@nom,'F')", Con)

                                    Cmd.Parameters.Add("@numCompte", SqlDbType.VarChar)
                                    Cmd.Parameters("@numCompte").Value = objVirement.numCompte
                                    Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinataire").Value = objVirement.banqueDestinataire
                                    Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                    Cmd.Parameters("@banqueDestinateur").Value = objVirement.banqueDestinateur
                                    Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                                    Cmd.Parameters("@nom").Value = objVirement.nom

                                    open()
                                    Cmd.ExecuteNonQuery()
                                    cnClose()
                                    objVirement = Nothing
                                End If
                            End If
                            'End


                            cnClose()
                            MsgBox("Montant Ajouté", MsgBoxStyle.Information, "Réussi !")
                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            affiche_paye()
                            total_paye()
                            cmbTypePay.SelectedIndex = 0
                            reste()


                        Catch ex As Exception
                            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
                            cnClose()
                        End Try
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            If List_tous.SelectedItems.Count > 0 Then
                idPai = CInt(List_tous.SelectedItems(0).SubItems(4).Text)

                Dim n As Integer
                With List_tous
                    If .SelectedIndices.Count > 0 Then
                        For i As Integer = 0 To .SelectedIndices.Count - 1
                            n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                            modif = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                        Next
                    End If
                End With
                txt_mont_paye.Clear()
                txt_remarq_paye.Clear()
                'Check_Autre.Checked = False
                'Check_Cheque.Checked = False
                'Check_Espece.Checked = False


                Cmd = New SqlCommand("Select * From Fournisseurs_Paiement where id_piem =" & n, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Dr.Read()
                If Dr.HasRows Then
                    txt_mont_paye.Text = Dr("Montant")
                    Date_paye.Text = Dr("Date_m")
                    txt_remarq_paye.Text = Dr("Remarque")
                    cmbTypePay.SelectedItem = Dr("Type_Paiement")
                End If
                Dr.Close()

                'remplir le cheque et le nominatif
                Dim typePai As String = ""
                If List_tous.SelectedItems(0).SubItems(2).Text = "Chéque" Then
                    typePai = "nominCheq"
                    Cmd = New SqlCommand("select * from cheques where idPai= " & idPai, Con)
                    open()
                    Dr = Cmd.ExecuteReader
                    If Dr.HasRows Then
                        Dr.Read()
                        With cheq
                            .idPai = Dr.GetInt32(0)
                            .numChe = Dr.GetString(1)
                            .nominatif = Dr.GetBoolean(2)
                            .dteRetra = convertNullToDate(Dr(3))
                            .dateChequ = Dr(4)
                            .banqueDestinataire = Dr("banqueDestinataire")
                            .banqueDestinateur = Dr("banqueDestinateur")
                            .typePersonne = "F"
                        End With
                    End If
                    Dr.Close()
                ElseIf List_tous.SelectedItems(0).SubItems(2).Text = "Effet" Then
                    typePai = "nominEffet"
                    Cmd = New SqlCommand("select * from Effet where idPai= " & idPai, Con)
                    open()
                    Dr = Cmd.ExecuteReader
                    If Dr.HasRows Then
                        Dr.Read()
                        With effetObj
                            .idPai = Dr.GetInt32(0)
                            .numEffet = Dr.GetString(1)
                            .dteEffet = Dr(2)
                            .dteRetrait = convertNullToDate(Dr(3))
                            .dateEcheance = Dr(4)
                            .nominatif = Dr.GetBoolean(5)
                            .banqueDestinataire = Dr("banqueDestinataire")
                            .banqueDestinateur = Dr("banqueDestinateur")
                            .typePersonne = "F"
                        End With
                    End If
                    Dr.Close()
                ElseIf List_tous.SelectedItems(0).SubItems(2).Text = "Virement" Then
                    Cmd = New SqlCommand("select * from virement where idPai= " & idPai, Con)
                    open()
                    Dr = Cmd.ExecuteReader
                    If Dr.HasRows Then
                        Dr.Read()
                        With objVirement
                            .idPai = Dr.GetInt32(0)
                            .numCompte = Dr.GetString(1)
                            .banqueDestinataire = Dr.GetString(2)
                            .banqueDestinateur = Dr.GetString(3)
                            .dateVirement = Dr(4)
                            .nom = Dr.GetString(5)
                            .typePersonne = "F"
                        End With
                    End If
                    Dr.Close()
                End If
                If cheq.numChe <> Nothing Or effetObj.numEffet <> Nothing Then
                    Cmd = New SqlCommand("select * from nominatifA " &
                                        "where idNomin in (select " & typePai & ".idNomin from " & typePai & " where " & typePai & ".idPai = " & idPai & ")", Con)
                    Dr = Cmd.ExecuteReader
                    If Dr.HasRows Then
                        Dr.Read()
                        With nomin
                            .idNomin = Dr.GetInt32(0)
                            .idPai = cheq.idPai
                            .nom = Dr.GetString(1)
                            .prenom = Dr.GetString(2)
                        End With
                    End If
                    Dr.Close()
                    cnClose()
                End If
                'end remplissage
                cnClose()
            Else
                'Tarik
                txt_mont_paye.Clear()
                txt_remarq_paye.Clear()
                cmbTypePay.SelectedIndex = 0
                txt_total_paye.Clear()
                txt_rest.Clear()
                cheq = Nothing
                effetObj = Nothing
                nomin = Nothing
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Dim idPai As Integer
    Private Sub btn_modif_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_modif_paye.Click
        Try
            If List_tous.SelectedItems.Count > 0 Then
                If modif <> 0 Then
                    If txt_mont_paye.Text <> "" Then
                        If cmbTypePay.SelectedIndex = 1 Then
                            If IsNothing(cheq.numChe) Then
                                MsgBox("Vous n'avez pas rempli le Chéque!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If
                        If cmbTypePay.SelectedIndex = 2 Then
                            If IsNothing(effetObj.numEffet) Then
                                MsgBox("Vous n'avez pas rempli l'Effet!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If

                        If cmbTypePay.SelectedIndex = 3 Then
                            If IsNothing(objVirement.numCompte) Then
                                MsgBox("Vous n'avez pas rempli le virement!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If
                        If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then


                            'Tarik
                            'Modifier le paiement dans la table paiement qui a une relation avec le Chéque et l'effet
                            Cmd = New SqlCommand("UPDATE [Salle_Mariage].[dbo].[paiements]" &
                                                "SET [montant] = @montant" &
                                                ",[type] = @type " &
                                                "WHERE idPai = " & idPai, Con)
                            Cmd.Parameters.Add("@montant", SqlDbType.Float)
                            Cmd.Parameters("@montant").Value = Math.Round(CDbl(txt_mont_paye.Text), 2)
                            Cmd.Parameters.Add("@type", SqlDbType.VarChar)
                            Cmd.Parameters("@type").Value = cmbTypePay.Text
                            open()
                            Cmd.ExecuteNonQuery()
                            'End 

                            If List_tous.SelectedItems(0).SubItems(2).Text = "Chéque" Or List_tous.SelectedItems(0).SubItems(2).Text = "Effet" Then
                                If cmbTypePay.SelectedIndex = 0 Then

                                    Dim str As String
                                    If List_tous.SelectedItems(0).SubItems(2).Text = "Chéque" Then
                                        str = "nominCheq"
                                        cheq = Nothing
                                    ElseIf List_tous.SelectedItems(0).SubItems(2).Text = "Effet" Then
                                        str = "nominEffet"
                                        effetObj = Nothing
                                    End If

                                    Cmd.Parameters.Clear()
                                    Cmd.CommandText = "delete from nominatifA" &
                                        " where idNomin in (select idNomin from " & str & " where idPai = " & idPai & ")"
                                    Cmd.ExecuteNonQuery()
                                    nomin = Nothing
                                    If List_tous.SelectedItems(0).SubItems(2).Text = "Chéque" Then
                                        str = "cheques"
                                    ElseIf List_tous.SelectedItems(0).SubItems(2).Text = "Effet" Then
                                        str = "Effet"
                                    End If
                                    Cmd.CommandText = "delete from " & str & " where idPai = " & List_tous.SelectedItems(0).SubItems(4).Text
                                    Cmd.ExecuteNonQuery()
                                End If
                            End If


                            Cmd = New SqlCommand("update Fournisseurs_Paiement set Montant=@Montant,Date_m=@Date_m,Remarque=@Remarque,Type_Paiement=@Type_Paiement" & _
                            " Where id_piem=" & modif, Con)
                            Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = txt_mont_paye.Text
                            Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_paye.Value.Date
                            Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_paye.Text
                            Cmd.Parameters.Add("@Type_Paiement", SqlDbType.VarChar).Value = cmbTypePay.Text
                            Cmd.ExecuteNonQuery()


                            'Tarik
                            Dim idNomin As Integer 'pour stocker la valeur de l'idNominatif ajout�
                            If Not IsNothing(nomin.nom) Then
                                Cmd.CommandText = "select idNomin from nominatifA where idNomin = " & nomin.idNomin
                                idNomin = Cmd.ExecuteScalar

                                If idNomin = 0 Then
                                    Cmd.Parameters.Clear()
                                    'cette partie ajoute le nominatif et retourne l'id de ce dernier
                                    Cmd.CommandText = "ajouterNominatif"
                                    Cmd.CommandType = CommandType.StoredProcedure
                                    Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                                    Cmd.Parameters("@nom").Value = nomin.nom
                                    Cmd.Parameters.Add("@Prenom", SqlDbType.VarChar)
                                    Cmd.Parameters("@Prenom").Value = nomin.prenom
                                    Cmd.Parameters.Add("@idNomin", SqlDbType.Int)
                                    Cmd.Parameters("@idNomin").Direction = ParameterDirection.Output

                                    Cmd.ExecuteNonQuery()
                                    idNomin = Cmd.Parameters("@idNomin").Value
                                End If
                            End If

                            'modifier le Chéque ou l'effet
                            Dim idChequ As Integer
                            If cmbTypePay.SelectedIndex = 2 Then
                                If Not IsNothing(effetObj.idPai) Then
                                    Cmd.CommandText = "select * from Effet where idPai = " & effetObj.idPai
                                    idChequ = Cmd.ExecuteScalar
                                    If IsNothing(idChequ) Or idChequ = 0 Then
                                        Cmd = New SqlCommand("INSERT INTO [Effet] " & _
                                                 "VALUES(" & idPai & ",@numChe,'" & Format(effetObj.dteEffet, "MM-dd-yyyy") & "'," & nullPourDate(effetObj.dteRetrait) & ",'" & Format(effetObj.dateEcheance, "MM-dd-yyyy") & "'," & convertToBit(cheq.nominatif) & ",@banqueDestinataire,@banqueDestinateur,'F')", Con)
                                        'MsgBox(cheq.dteRetra)
                                        Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                                        Cmd.Parameters("@numChe").Value = effetObj.numEffet

                                        Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinataire").Value = effetObj.banqueDestinataire

                                        Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinateur").Value = effetObj.banqueDestinateur
                                        Cmd.ExecuteNonQuery()

                                        If Not IsNothing(nomin.nom) Then
                                            Cmd.CommandText = "insert into nominEffet values(" & idNomin & "," & idPai & ")"
                                            Cmd.ExecuteNonQuery()
                                        End If
                                    End If
                                End If
                            ElseIf cmbTypePay.SelectedIndex = 1 Then
                                If Not IsNothing(cheq.idPai) Then
                                    Cmd.CommandText = "select * from cheques where idPai = " & cheq.idPai
                                    idChequ = Cmd.ExecuteScalar
                                    If idChequ = 0 Then
                                        Cmd = New SqlCommand("INSERT INTO [cheques] " & _
                                                 "VALUES(" & idPai & ",@numChe," & convertToBit(cheq.nominatif) & "," & nullPourDate(cheq.dteRetra) & ",'" & Format(cheq.dateChequ, "MM-dd-yyyy") & "',@banqueDestinataire,@banqueDestinateur,'F')", Con)
                                        'MsgBox(cheq.dteRetra)
                                        Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                                        Cmd.Parameters("@numChe").Value = cheq.numChe
                                        Cmd.Parameters.Add("@banque", SqlDbType.VarChar)
                                        Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinataire").Value = effetObj.banqueDestinataire

                                        Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinateur").Value = effetObj.banqueDestinataire
                                        Cmd.ExecuteNonQuery()

                                        If Not IsNothing(nomin.nom) Then
                                            Cmd.CommandText = "insert into nominCheq values(" & idNomin & "," & idPai & ")"
                                            Cmd.ExecuteNonQuery()
                                        End If
                                    End If
                                End If
                            ElseIf cmbTypePay.SelectedIndex = 3 Then
                                If Not IsNothing(objVirement.numCompte) Then
                                    Cmd.CommandText = "select * from virement where idPai = " & objVirement.idPai
                                    idChequ = Cmd.ExecuteScalar
                                    If idChequ = 0 Then
                                        Cmd = New SqlCommand("INSERT INTO [virement] " &
                                                    "VALUES (" & idPai & ",@numCompte,@banqueDestinataire, " &
                                                    "@banqueDestinateur,'" & Format(objVirement.dateVirement, "MM-dd-yyyy") & "',@nom,'F')", Con)

                                        Cmd.Parameters.Add("@numCompte", SqlDbType.VarChar)
                                        Cmd.Parameters("@numCompte").Value = objVirement.numCompte
                                        Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinataire").Value = objVirement.banqueDestinataire
                                        Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                                        Cmd.Parameters("@banqueDestinateur").Value = objVirement.banqueDestinateur
                                        Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                                        Cmd.Parameters("@nom").Value = objVirement.nom
                                        Cmd.ExecuteNonQuery()

                                        objVirement = Nothing
                                    End If
                                End If
                            End If
                            'End
                            cnClose()

                            modif = 0
                            MsgBox("Montant Modifier ", MsgBoxStyle.Information, "R�ussi !")
                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            affiche_paye()
                            total_paye()
                            reste()
                        End If
                    End If
                End If
            Else
                MsgBox("Vous devez choisir un paiement de la la liste!!!", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub

    Private Sub btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_rech.Click
        Frm_liste_Demande.MdiParent = Frm_Main
        Frm_liste_Demande.Show()
        liste = "demande fornisseur paiement"

    End Sub

    Private Sub txt_client_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_client.TextChanged
        If txt_client.Text <> "" Then
            '--------------------------------------------
            txt_mont_paye.Clear()
            txt_remarq_paye.Clear()
            txt_total_paye.Clear()
            txt_rest.Clear()
            txt_prix.Text = 0
            List_tous.Items.Clear()

            Try
                Comb_Nom_Fournisseurs.Items.Clear()
                Comb_catégor.Items.Clear()
                Cmd = New SqlCommand("Select * From Fournisseus,Fournisseurs_Demande where Fournisseus.Id_Fournisseur=Fournisseurs_Demande.Id_Fournisseur and id_demande_client=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    If Dr("Type_Fournisseur") = "Entreprise" Then
                        Comb_Nom_Fournisseurs.Items.Add(Dr("Nom_entreprise"))
                    Else
                        Comb_Nom_Fournisseurs.Items.Add(Dr("Nom") & "  " & Dr("Prenom"))
                    End If
                Loop
                Dr.Close()
                cnClose()
                If Comb_Nom_Fournisseurs.Items.Count > 0 Then
                    Comb_Nom_Fournisseurs.SelectedIndex = 0
                End If

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '-----------------------------------------------------

            'If Comb_Nom_Fournisseurs.Text <> "" Then
            '    affiche_paye()
            '    total_paye()
            '    reste()
            'End If
        End If
    End Sub

    Private Sub cmbTypePay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTypePay.SelectedIndexChanged
        If cmbTypePay.SelectedIndex = 1 Then
            btnCheqFrm.Visible = True
        Else
            btnCheqFrm.Visible = False
        End If

        If cmbTypePay.SelectedIndex = 2 Then
            btnEffet.Visible = True
        Else
            btnEffet.Visible = False
        End If

        If cmbTypePay.SelectedIndex = 3 Then
            btnVirementFrm.Visible = True
        Else
            btnVirementFrm.Visible = False
        End If
    End Sub

    Private Sub btnCheqFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheqFrm.Click
        Try
            If Comb_Nom_Fournisseurs.Text <> "" Then
                If List_tous.SelectedItems.Count <= 0 Then
                    cheq = Nothing
                    nomin = Nothing
                End If
                Me.Enabled = False
                frm = Me
                frmCheques.Show()
            Else
                MsgBox("Vous devez choisir un fournisseurs!!!", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub

    Private Sub btnEffet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEffet.Click
        If List_tous.SelectedItems.Count <= 0 Then
            effetObj = Nothing
            nomin = Nothing
        End If
        Me.Enabled = False
        frm = Me
        FrmEffet.Show()
    End Sub

    Private Sub btnVirementFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVirementFrm.Click
        If List_tous.SelectedItems.Count <= 0 Then
            objVirement = Nothing
        End If
        Me.Enabled = False
        frm = Me
        frmVirement.Show()
    End Sub
End Class